package org.vectrics.recruiter.struts.tag;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.SystemException;
import org.vectrics.VectricsException;
import org.vectrics.common.db.DataUtils;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.TextHelper;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.JobAgent;
import org.vectrics.recruiter.user.JobAgentLocation;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;
import org.vectrics.user.User;

public class UserJobAgentListTag extends VectricsTagSupport {
    
	private static final long serialVersionUID = -3749194332663822770L;

	static private Category log = Category.getInstance(UserJobAgentListTag.class);
    private String personId = null;
    private HttpServletRequest request = null;
    private String showJobSelection = "false";
    private String compact = null;
    private String activeOnly = "false";
    private String width = "90%";
    
	public String makeStartTag()  {
		ThreadProperties.getSession().clear();
		ThreadProperties.getSession().evict(CandidateInfo.class);
		ThreadProperties.getSession().evict(JobAgent.class);
		ThreadProperties.getSession().getSessionFactory().evict(Person.class);
		ThreadProperties.getSession().getSessionFactory().evict(User.class);
		ThreadProperties.getSession().getSessionFactory().evict(JobAgent.class);
		ThreadProperties.getSession().getSessionFactory().evict(CandidateInfo.class);
		
		StringBuffer html = new StringBuffer();
		
		log.info("doStartTag() called");
		request = (HttpServletRequest)super.pageContext.getRequest();
		String contextPath = request.getContextPath();
		if (personId == null) {
			log.error("userId property not set before rendering");
			throw new SystemException("userId property not set before rendering");
		}
		
		try {
			Person user = CoreServiceLocator.getPersonService().findPersonById(new Long(personId));
			CandidateInfo candidateInfo = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(user);
			if (candidateInfo != null) {
				Collection agents = DataUtils.removeVoided(candidateInfo.getJobAgents());
				displayAgents(html, agents);
			} else {
				displayAgents(html, new ArrayList());
			    
			}
		}
		catch (Exception de) {
			log.error("DataException finding jobs", de);
			throw new SystemException("DataException finding jobs", de);
		}
		
		return(html.toString());
	}

		
	private void displayAgents(StringBuffer html, Collection agents) throws VectricsException {
		log.debug("displayAgents() called");
		BorderTable table = new BorderTable();
		table.setWidth(width);
		Row row = table.addRow();
        row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
		Cell cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "Agent Name"));

		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "Phrase"));
		
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "Locations"));
		
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", ""));
			
		Iterator iter = agents.iterator();
		if (iter.hasNext()) {
				
			while (iter.hasNext()) {
				JobAgent agent = (JobAgent)iter.next();
				row = table.addRow();
				cell = row.addCell();
				cell.addElement(TextHelper.getFont("subFont", agent.getName()));
				
				cell = row.addCell();
				cell.addElement(TextHelper.getFont("subFont", agent.getSearchPhrase()));
				
				cell = row.addCell();
				Collection locations = agent.getLocations();
				Iterator locationIter = locations.iterator();
				String locationHtml = "";
				while (locationIter.hasNext()) {
					JobAgentLocation location = (JobAgentLocation)locationIter.next();
					locationHtml += location.getLocation().getName();
					if (locationIter.hasNext())
						locationHtml +="<br>";
				}
				cell.addElement(TextHelper.getFont("subFont", locationHtml));
				
				cell = row.addCell();
				A link = new A();
				link.setClass("subLink");
				link.setHref(getContextPath() + "/profile/" + ThreadProperties.getUriDomainId() + "/agent/edit.do?jobAgentId=" + agent.getId() + "&personId=" + agent.getCandidateInfo().getPerson().getId());
				link.addElement("edit");
				cell.addElement(link);
	
			}
		} else {
			row = table.addRow();
			cell = row.addCell();
			cell.setColSpan(4);
			cell.addElement(TextHelper.getFont("subFont", "<i>"  + this.getPhrase("phrase.no.job.agents.found") + "</i>"));
		}
		html.append(table.toString());
	}
	

	/**
	 * @return Returns the userId.
	 */
	public String getPersonId() {
		return personId;
	}
	/**
	 * @param userId The userId to set.
	 */
	public void setPersonId(String personId) {
		this.personId = personId;
	}
	
	/**
	 * @return Returns the width.
	 */
	public String getWidth() {
		return width;
	}
	/**
	 * @param width The width to set.
	 */
	public void setWidth(String width) {
		this.width = width;
	}
}
